package com.lwking.scan;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import com.olc.scan.ScanManager;

import java.util.Objects;

public class P25ScanPlugin extends CordovaPlugin {

    ScanManager sm;

    String m_Broadcastname = "com.barcode.sendBroadcast";

    MyCodeReceiver receiver = new MyCodeReceiver();

    static CallbackContext callback;

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        callback = callbackContext;
        switch (action) {
            case "init":
                initScan();
                return true;
            case "open":
                openScan();
                return true;
        }
        callback.error("not method");
        return false;
    }

    private void openScan() {
        Intent intent = new Intent();
        intent.setAction("com.barcode.sendBroadcastScan");
        cordova.getActivity().sendBroadcast(intent);
    }

    public class MyCodeReceiver extends BroadcastReceiver {
        private static final String TAG = "MycodeReceiver";

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(m_Broadcastname)) {
                String str = intent.getStringExtra("BARCODE");
                if (!"".equals(str)) {
                    System.out.println(str);
                    callback.success(str);
                }
            }
        }
    }

    public void registerBroadcast() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(m_Broadcastname);
        cordova.getActivity().registerReceiver(receiver, intentFilter);
    }

    /**
     * 初始化扫描
     */
    @SuppressLint("WrongConstant")
    public void initScan() {
        sm = (ScanManager) cordova.getActivity().getSystemService("olc_service_scan");
        if (sm == null) {
            callback.error("没有检测到扫描设备");
            return;
        }

        // 设置左扫描键
        if (!sm.getScanSwitchLeft(cordova.getContext())) {
            sm.setScanSwitchLeft(true);
        }
        // 设置中间扫描键
        if (!sm.getScanSwitchMiddle(cordova.getContext())) {
            sm.setScanSwitchMiddle(true);
        }
        // 设置右扫描键
        if (!sm.getScanSwitchRight(cordova.getContext())) {
            sm.setScanSwitchRight(true);
        }
        // 设置扫描声音
        if (!sm.getScanSound(cordova.getContext())) {
            sm.setScanSound(true);
        }
        // 设置扫描震动
        if (!sm.getScanVibrate(cordova.getContext())) {
            sm.setScanVibrate(true);
        }
        // 设置扫描模式(0 : 普通; 1 : 连续扫描(自动); 2 :连续扫描(手动:按下不松开连续扫描，松开停止)
        sm.setScanOperatingMode(0);
        // 设置条码接收模式 state （0 : 快速接收; 1 : 慢速接收; 2 : 广播接收（示例程序默认为广播接收)
        sm.setBarcodeReceiveModel(2);
        // 设置当前条码结束符 State（0 : 不换行; 1 : 换行符换行; 2 : 回车换行）
        sm.setBarcodeSeparator(0);
        registerBroadcast();
    }

    @Override
    public void onResume(boolean multitasking) {
        super.onResume(multitasking);
        registerBroadcast();
    }

    @Override
    public void onPause(boolean multitasking) {
        // cordova.getActivity().unregisterReceiver(receiver);
        super.onPause(multitasking);
    }
}